package com.example.myapplication3;
import android.Manifest;
import android.app.SearchManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.example.myapplication3.bean.Student;
import com.example.myapplication3.bean.Teacher;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    public static final int CODE = 1;
    protected EditText mEdit;
    private Button mButton;

    private Button mButton1;
    private Button mButton2;
    private Button mButtonCall;
    private Button mButtonDial;
    private Button mButtonHistory;
    private Button mButtonSendTo;
    private Button mButtonView;
    private Button mButtonWebSearch;
    private Button mButtonSend;
    private Button mButtonCapture;
    private ImageView mPhoto;
    private ActivityResultLauncher mActivityResultLauncher;
    private Button mButtonData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        Log.d("test123", "============onCreate============");
        mEdit = findViewById(R.id.edit);
        mButton = findViewById(R.id.button);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.jn.test345");
                intent.putExtra("name", mEdit.getText().toString());
                startActivity(intent);
            }
        });
        mButton1 = findViewById(R.id.button1);
        mButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                ComponentName componentName = new ComponentName("com.example.myapplication2",
                        "com.example.myapplication2.LayoutActivity");
                intent.setComponent(componentName);
                intent.putExtra("name", mEdit.getText().toString());
                startActivity(intent);
            }
        });
        mButton2 = findViewById(R.id.button2);
        mButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClassName("com.example.myapplication2",
                        "com.example.myapplication2.LayoutActivity");
                intent.putExtra("name", mEdit.getText().toString());
                startActivity(intent);
            }
        });
        mButtonCall = findViewById(R.id.button_call);
        mButtonCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE)
                        != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(
                            MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, CODE);
                } else {
                    callPhone();
                }
            }
        });
        mButtonDial = findViewById(R.id.button_dial);
        mButtonDial.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:10086"));
                startActivity(intent);
            }
        });
        mButtonHistory = findViewById(R.id.button_history);
        mButtonHistory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL_BUTTON);
                startActivity(intent);
            }
        });
        mButtonSendTo = findViewById(R.id.button_sendto);
        mButtonSendTo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //发送邮件
                /*Intent intent = new Intent();
                intent.setAction(Intent.ACTION_SENDTO);
                intent.setData(Uri.parse("mailto:1234567@qq.com"));
                intent.putExtra(Intent.EXTRA_SUBJECT, "你好");
                intent.putExtra(Intent.EXTRA_TEXT, "今天有空吗");
                startActivity(intent);*/
                //发送短信
                Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:10010"));
                intent.putExtra("sms_body", "测试短信内容");
                startActivity(intent);
            }
        });
        mButtonView = findViewById(R.id.button_view);
        mButtonView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Uri uri = Uri.parse("http://www.baidu.com");
                //Uri uri = Uri.parse("tel:10010");
                Uri uri = Uri.parse("geo:39.899533,116.036476");
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }
        });
        mButtonWebSearch = findViewById(R.id.button_web_search);
        mButtonWebSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
                intent.putExtra(SearchManager.QUERY, "JLASTU");
                startActivity(intent);
            }
        });
        mButtonSend = findViewById(R.id.button_send);
        mButtonSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_TEXT, "HELLO");
                startActivity(Intent.createChooser(intent, "test"));
            }
        });
        mButtonCapture = findViewById(R.id.button_capture);
        mPhoto = findViewById(R.id.photo);
        mActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
                new ActivityResultCallback<ActivityResult>() {
                    @Override
                    public void onActivityResult(ActivityResult result) {
                        if(result.getResultCode() == RESULT_OK) {
                            Intent intent = result.getData();
                            Bitmap bitmap = intent.getParcelableExtra("data");
                            mPhoto.setImageBitmap(bitmap);
                        }
                    }
                });
        mButtonCapture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                mActivityResultLauncher.launch(intent);
            }
        });
        mButtonData = findViewById(R.id.button_data);
        mButtonData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                intent.putExtra("boolean", true);
                byte b = 1;
                intent.putExtra("byte", b);
                intent.putExtra("char", 'a');
                short s = 2;
                intent.putExtra("short", s);
                intent.putExtra("int", 3);
                intent.putExtra("long", 4L);
                intent.putExtra("float", 1.23F);
                intent.putExtra("double", 3.45);

                Bundle bundle = new Bundle();
                bundle.putInt("int", 123);
                bundle.putBoolean("boolean", true);
                intent.putExtra("bundle", bundle);
                int intArray[] = new int[] {2, 5, 8, 9};
                intent.putExtra("intArray", intArray);

                ArrayList<String> list = new ArrayList<>();
                list.add("a");
                list.add("b");
                list.add("c");
                intent.putStringArrayListExtra("list",list);

                Student student = new Student();
                student.setName("jim");
                student.setAge(20);
                student.setNumber(2020111111);
                intent.putExtra("student", student);

                Teacher teacher = new Teacher();
                teacher.setName("tom");
                teacher.setAge(66);
                teacher.setNumber(333333);
                intent.putExtra("teacher", teacher);
                startActivity(intent);
            }
        });


    }

    private void callPhone() {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:10000"));
        startActivity(intent);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == CODE) {
            if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                callPhone();
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("test123", "============onStart============");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("test123", "============onRestart============");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("test123", "============onResume============");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("test123", "============onPause============");

    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("test123", "============onStop============");
        mEdit.setText("");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("test123", "============onDestroy============");
    }
}